#include <bits/stdc++.h>
// 2024/11/22
// tag: 
using namespace std;

class edge{
public:
    int to, relevance;
    edge(const int& t, const int& r){
        to = t, relevance = r;
    }
};

int n, q, s[5001][5001], vis[5001];
vector<edge> tree[5001];

void dfs(int x){
    if (vis[x])
        return;
    vis[x] = true;
    for (auto p : tree[x])
    {
        dfs(p.to);
        for (int i = 1; i <= n; i++)
            if (s[p.to][i] != -1)
                s[x][i] = min({s[x][i], s[p.to][i], s[x][p.to]});
    }
}

signed main()
{
    cin >> n >> q;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            s[i][j] = -1;
    for (int i = 1; i <= n; i++)
        s[i][i] = INT_MAX;
    for (int i = 1; i < n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        tree[a].push_back(edge(b, c));
        tree[b].push_back(edge(a, c));
        s[a][b] = c;
        s[b][a] = c;
    }
    dfs(1);
    cout << endl;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            cout << s[i][j] << " ";
        cout << endl;
    }
    for (int i = 1; i <= q; i++)
    {
        int k, v, cnt = 0;
        cin >> k >> v;
        for (int j = 1; j <= n; j++)
            cnt += (v != j && s[v][j] >= k);
        cout << cnt << endl;
    }
    return 0;
}

/*
#include <bits/stdc++.h>

using namespace std;

int main() {
	freopen("mootube.in", "r", stdin);
	// Read in first line: n = number of videos, q = number of questions
	int n, q;
	cin >> n >> q;
	// Create adjacency list
	vector<vector<pair<int, int>>> adj(n + 1);
	// For the next n lines, read in node neighbors and relevance weights
	for (int ln = 1; ln < n; ln++) {
		int a, b, r;
		cin >> a >> b >> r;
		adj[a].push_back({b, r});
		adj[b].push_back({a, r});
	}

	freopen("mootube.out", "w", stdout);
	// For the next q lines, read in the threshold and starting node
	for (int ln = 0; ln < q; ++ln) {
		int r, v;
		cin >> r >> v;

		// Search for the # of videos that can be reached with >= (r)elevance
		int audienceReach = 0;
		bitset<5001> visited;
		visited[v] = true;
		queue<int> q;
		q.push(v);
		while (!q.empty()) {
			int cur = q.front();
			q.pop();
			for (auto& [nxt, w] : adj[cur]) {
				if (!visited[nxt] && w >= r) {
					visited[nxt] = true;
					++audienceReach;
					q.push(nxt);
				}
			}
		}

		// Write the audience reach to the output file for the current query
		cout << audienceReach << endl;
	}
}
*/

/*
#include <bits/stdc++.h>
using namespace std;

vector<vector<pair<int, int>>> neighbors;
vector<bool> visited;
int threshold;
int num_reachable;

void search_videos(int v) {
	visited[v] = true;
	for (const pair<int, int> &n : neighbors[v]) {
		if (!visited[n.first] && n.second >= threshold) {
			num_reachable++;
			search_videos(n.first);
		}
	}
}

int main() {
	freopen("mootube.in", "r", stdin);
	int video_num;
	int query_num;
	cin >> video_num >> query_num;

	neighbors = vector<vector<pair<int, int>>>(video_num);
	for (int e = 0; e < video_num - 1; e++) {
		int a, b;
		int relevance;
		cin >> a >> b >> relevance;
		a--;
		b--;
		neighbors[a].push_back({b, relevance});
		neighbors[b].push_back({a, relevance});
	}

	freopen("mootube.out", "w", stdout);
	for (int q = 0; q < query_num; q++) {
		int start;
		cin >> threshold >> start;
		start--;

		// reset our global variables for the current query
		num_reachable = 0;
		visited = vector<bool>(video_num);
		search_videos(start);

		cout << num_reachable << '\n';
	}
}
*/